package love.yuanmeng.guetcoalsystem.cotroller;

import love.yuanmeng.guetcoalsystem.pojo.CBond;
import love.yuanmeng.guetcoalsystem.service.CBondService;
import love.yuanmeng.guetcoalsystem.util.DataGridView;
import love.yuanmeng.guetcoalsystem.util.ResultObj;
import love.yuanmeng.guetcoalsystem.vo.PayBondVo;
import love.yuanmeng.guetcoalsystem.vo.PropIdOrderIdVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/10/16 21:44
 */
@Controller
@CrossOrigin
@RequestMapping("cbond")
public class CBondController {
    @Autowired
    CBondService cBondService;

    /**
     * 买方缴纳定金
     */
    @ResponseBody
    @RequestMapping("/payBond")
    public ResultObj payBond(@RequestBody PayBondVo payBondVo) {
        try {
            int i = cBondService.updatePropAndOrderForBond(payBondVo.getEnId(), payBondVo.getPropId(), payBondVo.getOrderId());
            if (i == 1)
                return ResultObj.OPERATE_SUCCESS;
            else return ResultObj.OPERATE_ERROR;
        } catch (Exception e) {
            return ResultObj.OPERATE_ERROR;
        }

    }

    /**
     * 买方查询自己的资金
     */
    @ResponseBody
    @RequestMapping("/getBond")
    public DataGridView getBond(@Param("enId") String enId) {
        try {
            CBond cBond = cBondService.selectCBondById(enId);
            BigDecimal bd1 = new BigDecimal(cBond.getContractFrozenAmount());
            cBond.setContractFrozenAmount(bd1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
            bd1 = new BigDecimal(cBond.getUnfrozenAmount());
            cBond.setUnfrozenAmount(bd1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
            bd1 = new BigDecimal(cBond.getAccountBalance());
            cBond.setAccountBalance(bd1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());

            return new DataGridView(200, cBond);
        } catch (Exception e) {

            return new DataGridView(-1, null);
        }

    }


    /**
     * 买方撤销订单
     */
    @ResponseBody
    @RequestMapping("/cancelOrder")
    public ResultObj cancelOrder(@RequestBody PropIdOrderIdVo propIdOrderIdVo) {
        try {
            int i = cBondService.updatePropAndDeleteOrder(propIdOrderIdVo);
            if (i == 1)
                return ResultObj.OPERATE_SUCCESS;
            else return ResultObj.OPERATE_ERROR;
        } catch (Exception e) {
            return ResultObj.OPERATE_ERROR;
        }


    }

}
